package com.itboye.sunsun.luntan;

import java.util.ArrayList;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.OrderViewPagerAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.luntan.fragment.HuiFuWoDeFragment;
import com.itboye.sunsun.luntan.fragment.PingLunWoDeFragment;
import com.itboye.sunsun.luntan.fragment.WoDeHuiTieFragment;
import com.itboye.sunsun.luntan.fragment.WoDePingLunFragment;
import com.itboye.sunsun.luntan.fragment.WoDeZhuTiFragment;
import com.itboye.sunsun.person.fragments.AllFragment;
import com.itboye.sunsun.person.fragments.PayFragment;
import com.itboye.sunsun.person.fragments.ReceiveFragment;
import com.itboye.sunsun.person.fragments.SendFragment;
import com.itboye.sunsun.person.ui.MyOrderActivity.MyOnPageChangeListener;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.umeng.common.message.Log;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class WoDeTieZiActivity extends BaseActivity implements OnClickListener {

	ViewPager viewpager;
	TextView fatie;
	TextView huifu;
	private ArrayList<Fragment> fragmentList;

	private ImageView search;
	TextView woDeZhuiTi;
	TextView woDeHuiTie;
	TextView huiFuWoDe;
	TextView pingLunWoDe;
	TextView woDePingLun;
	TextView[] textviews = new TextView[5];
	boolean isReadOne, isReadTwo;
	int currentPage = 0;
	Intent intent;
	ImageView tishi1,tishi2;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_wode_tiezi);
		setStatusBarColor(R.color.top_blue);
		isReadOne = (boolean) SPUtils.get(this, null, SPContants.IS_READ_ONE,
				false);
		isReadTwo = (boolean) SPUtils.get(this, null, SPContants.IS_READ_TWO,
				false);
		if (isReadOne) {
			tishi1.setVisibility(View.VISIBLE);
//			huiFuWoDe.setBackgroundResource(R.drawable.message1);
		}
		if (isReadTwo) {
			tishi2.setVisibility(View.VISIBLE);
//			pingLunWoDe.setBackgroundResource(R.drawable.message1);
		}
		intent = getIntent();
		currentPage = intent.getIntExtra("current", 0);
		IntentFilter filter = new IntentFilter("message");
		registerReceiver(broadcastReceiver, filter);
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});
		// message_1.setVisibility(View.GONE);
		// message_2.setVisibility(View.GONE);

		fragmentList = new ArrayList<Fragment>();
		fragmentList.add(new WoDeZhuTiFragment());
		fragmentList.add(new WoDeHuiTieFragment());
		fragmentList.add(new WoDePingLunFragment());
		fragmentList.add(new HuiFuWoDeFragment());
		fragmentList.add(new PingLunWoDeFragment());
		viewpager.setAdapter(new OrderViewPagerAdapter(
				getSupportFragmentManager(), fragmentList));

		viewpager.setCurrentItem(currentPage);
		viewpager.setOnPageChangeListener(new MyOnPageChangeListener());
		textviews[0] = woDeZhuiTi;
		textviews[1] = woDeHuiTie;
		textviews[2] = woDePingLun;
		textviews[3] = huiFuWoDe;
		textviews[4] = pingLunWoDe;
		for (int i = 0; i < textviews.length; i++) {
			if (i == currentPage) {
				textviews[i].setTextColor(getResources().getColor(
						R.color.top_blue));
			} else {
				textviews[i].setTextColor(Color.BLACK);
			}
		}
	}

	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			System.out.println(">>>接收到广播");
			if (intent.getIntExtra("message_type", 0) == 0) {
				tishi1.setVisibility(View.VISIBLE);
//				huiFuWoDe.setBackgroundResource(R.drawable.message1);
			}
			if (intent.getIntExtra("message_type", 0) == 1) {
				tishi2.setVisibility(View.VISIBLE);
//				pingLunWoDe.setBackgroundResource(R.drawable.message1);
			}
		}
	};

	class OrderViewPagerAdapter extends FragmentPagerAdapter {
		ArrayList<Fragment> list;

		public OrderViewPagerAdapter(FragmentManager fm,
				ArrayList<Fragment> list) {
			super(fm);
			this.list = list;

		}

		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageSelected(int arg0) {
			for (int i = 0; i < textviews.length; i++) {
				if (i == arg0) {
					textviews[i].setTextColor(getResources().getColor(
							R.color.top_blue));
				} else {
					textviews[i].setTextColor(Color.BLACK);
				}
			}
			// currIndex = arg0;
			if (arg0 == 3) {
				tishi1.setVisibility(View.GONE);
//				textviews[3].setBackgroundResource(R.drawable.message2);
				SPUtils.put(App.ctx, null, SPContants.IS_READ_ONE, false);
			}
			if (arg0 == 4) {
				tishi2.setVisibility(View.GONE);
//				textviews[4].setBackgroundResource(R.drawable.message2);
				SPUtils.put(App.ctx, null, SPContants.IS_READ_TWO, false);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.woDeZhuiTi:
			viewpager.setCurrentItem(0);
			break;
		case R.id.woDeHuiTie:
			viewpager.setCurrentItem(1);
			break;
		case R.id.woDePingLun:
			viewpager.setCurrentItem(2);
			break;
		case R.id.huiFuWoDe:
			viewpager.setCurrentItem(3);
			break;
		case R.id.pingLunWoDe:
			viewpager.setCurrentItem(4);
			break;
		case R.id.search:
			startActivity(new Intent(getApplicationContext(),
					LunTanSouSuoActivity.class));
			break;

		default:
			break;
		}
	}

}
